#coding = utf-8

from kuai.kuaiext import XYZ, PBC
from kuai.element import *

class Atom:
    def __init__(self, symbol="C", coords=XYZ(0.0, 0.0, 0.0), type="?"):
        self._symbol = symbol
        self.coords = coords
        self.type = type
        self.formal_charge = 0
        
    def symbol(self):
        return self._symbol
    
    def number(self):
        return get_number(self._symbol)
        

SINGLE_BOND = 2
DOUBLE_BOND = 4
TRIPLE_BOND = 6
PARTIAL_BOND = 3
        
class Bond:
    def __init__(self, atom1, atom2, order = SINGLE_BOND):
        self.atom1 = atom1
        self.atom2 = atom2
        self.order = order

class Molecule:
    def __init__(self, atoms, bonds):
        self.atoms = atoms[:]
        self.bonds = bonds[:]
        self.neighbors = {}
        for i in self.bonds:
            if i.atom1 not in self.atoms:
                self.atoms.append(i.atom1)
            if i.atom2 not in self.atoms:
                self.atoms.append(i.atom2)
                
        for i in self.atoms:
            self.neighbors[i] = []

        for i in self.bonds:
            self.neighbors[i.atom1].append((i.atom2, i))
            self.neighbors[i.atom2].append((i.atom1, i))

    def degree(self, atom):
        return len(self.neighbors[atom])
    
    def index(self, var):
        try:
            return self.atoms.index(var)
            return self.bonds.index(var)
        except ValueError:
            return -1
        if var in self.atoms:
            return 
        
    def formula(self):
        counter = {}
        for i in self.atoms:
            if i.symbol() in counter:
                counter[i.symbol()] += 1
            else:
                counter[i.symbol()] = 1
        result = ''
        if 'C' in counter:
            result += 'C'
            if counter['C'] > 1:
                result += str(counter['C'])
            del counter['C']
        if 'H' in counter:
            result += 'H'
            if counter['H'] > 1:
                result += str(counter['H'])
            del counter['H']
        return result

    def get_bond(self, atom1, atom2):
        for i in self.neighbors[atom1]:
            if i[0] is atom2:
                return i[1]
        return None

    def get_angle_center(self, a1, a2):
        for i in self.neighbors[a1]:
            for j in self.neighbors[a2]:
                if i[0] == j[0]:
                    return i[0]
        return None


def is_same_mol(mol1, mol2):
    if len(mol1.atoms) == len(mol2.atoms) \
       and len(mol1.bonds) == len(mol2.bonds):
        for i in range(len(mol1.atoms)):
            if mol1.atoms[i].symbol() != mol2.atoms[i].symbol():
                return False
        for i in range(len(mol1.bonds)):
            b1 = mol1.bonds[i]
            b2 = mol2.bonds[i]
            if mol1.index(b1.atom1) != mol2.index(b2.atom1) \
               or mol1.index(b1.atom2) != mol2.index(b2.atom2) \
               or b1.order != b2.order:
                return False
        return True
    else:
        return False
        
        

def formula_weight(formula):
    result = 0.0
    regex = r"[A-Z][a-z]?\d*"
    for i in range(len(formula)):
        if foi in range()



















